from django.core.management.base import BaseCommand
from user.models import User, UserProfile


class Command(BaseCommand):
    help = '为现有用户创建UserProfile'

    def handle(self, *args, **options):
        users = User.objects.all()
        created_count = 0
        
        for user in users:
            try:
                UserProfile.objects.get(user=user)
                self.stdout.write(f'用户 {user.username} 已有档案')
            except UserProfile.DoesNotExist:
                UserProfile.objects.create(user=user)
                created_count += 1
                self.stdout.write(f'为用户 {user.username} 创建档案')
        
        self.stdout.write(
            self.style.SUCCESS(f'成功为 {created_count} 个用户创建了档案')
        )
